package com.notepad.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import android.text.format.Time;

public class CommonUtils {
	private static ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
	private static Long[] tempTimeMillis = null;

	private static Date date = new Date();

	/**
	 * 获取格式化后的时间，格式为：mm:ss
	 * 
	 * @param timeMillis
	 *            时间，单位：毫秒
	 * @return 格式化为mm:ss的时间字符串
	 */
	public static String getFormattedDateString(long timeMillis) {
		date.setTime(timeMillis);
		return sdf.format(date);
	}

	public static String getFormattedDate(long timeMillis) {
		date.setTime(timeMillis);
		return sdf2.format(date);
	}

	public static String format(int hourOfDay) {
		String str = "" + hourOfDay;
		if (str.length() == 1) {
			str = "0" + str;
		}
		return str;
	}

	public static String toDateString(Calendar calendar) {
		String day = null;
		switch (calendar.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			day = "周一";
			break;
		case 2:
			day = "周二";
			break;
		case 3:
			day = "周三";
			break;
		case 4:
			day = "周四";
			break;
		case 5:
			day = "周五";
			break;
		case 6:
			day = "周六";
			break;
		case 7:
			day = "周天";
			break;
		}
		return calendar.get(Calendar.YEAR) + "年"
				+ CommonUtils.format(calendar.get(Calendar.MONTH) + 1) + "月"
				+ calendar.get(Calendar.DAY_OF_MONTH)  + "  星期" + day;
	}
	public static String timeTransfer(int i){
		Time time = new Time();
		long tempLong = Long.parseLong(arraylist.get(i).get("datetime"));
		tempTimeMillis[i] = tempLong;
		//日期
		time.setToNow();
		String dateNow = time.toString().substring(0,8);
		String timeNow = time.toString().substring(9,13);
		time.set(tempLong);
		String datePast = time.toString().substring(0,8);
		String timePast = time.toString().substring(9,13);
		long tempDate = Long.parseLong(dateNow)-Long.parseLong(datePast);
		long tempTime = Long.parseLong(timeNow)-Long.parseLong(timePast);
		if(tempDate == 0)
			return timePast.substring(0, 2)+":"+timePast.substring(2, 4);
		if(tempDate == 1)
			return "昨天";
		if(tempDate>=2)
			return datePast.substring(4,6)+"/"+datePast.substring(6,8);
		return null;
	}
	/**
	 * 
	 * @param i
	 * @return
	 */
	public static String timeTransfer(String i){
		Time time = new Time();
		time.setToNow();
		String dateNow = time.toString().substring(0,8);
		String timeNow = time.toString().substring(9,13);
		time.set(Long.parseLong(i));
		String dateSet = time.toString().substring(0,8);
		String timeSet = time.toString().substring(9,13);
		long tempDate = Long.parseLong(dateSet)-Long.parseLong(dateNow);
		long tempTime = Long.parseLong(timeSet)-Long.parseLong(timeNow);
		if(tempDate < 0 || (tempDate ==0 && tempTime<0))
			return "已过期";
		if(tempDate == 0 && tempTime > 0)
			return "今天"+timeSet.substring(0, 2)+":"+timeSet.substring(2, 4);
		if(tempDate == 1)
			return "明天"+timeSet.substring(0, 2)+":"+timeSet.substring(2, 4);
		if(tempDate == 2)
			return "后天"+timeSet.substring(0, 2)+":"+timeSet.substring(2, 4);
		if(tempDate > 2)
			return dateSet.substring(4,6)+"/"+dateSet.substring(6,8);
		return null;
	}
	
	/**
	 * 把某个毫秒数 按照要求 返回相应字符丿
	 * 
	 * 如果date是当天 那么返回  HH:mm
	 * 如果不是当天  返回星期n

	 * @param date
	 * @return
	 */
	public static String parse(long date) {
		//当前时间
		Calendar now=Calendar.getInstance();
		//date时间
		Calendar other=Calendar.getInstance();
		other.setTimeInMillis(date);
		//判断是不是同一天：
		if(now.get(Calendar.YEAR)==other.get(Calendar.YEAR)&&
				now.get(Calendar.DAY_OF_YEAR) == 
				other.get(Calendar.DAY_OF_YEAR)){
			//是同一天
			return new SimpleDateFormat("HH:mm").format(other.getTime());
		}
		//不是同一夿 则直接返囿  星期凿
		int day=other.get(Calendar.DAY_OF_WEEK);
		String weekday="";
		switch (day) {
		case Calendar.MONDAY:
			weekday="星期一";
			break;
		case Calendar.TUESDAY:
			weekday="星期二";
			break;
		case Calendar.WEDNESDAY:
			weekday="星期三";
			break;
		case Calendar.THURSDAY:
			weekday="星期四";
			break;
		case Calendar.FRIDAY:
			weekday="星期五";
			break;
		case Calendar.SATURDAY:
			weekday="星期六";
			break;
		case Calendar.SUNDAY:
			weekday="星期天";
			break;
		}
		return weekday;
	}

}

